﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;

namespace Manager.Controls
{
    public partial class ucServicePriceUpdate : System.Web.UI.UserControl
    {
        public bool IsNew
        {
            get
            {
                return Convert.ToBoolean(ViewState["IsNew"]);
            }
            set
            {
                ViewState["IsNew"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Load_Service();
            }
        }

        public void Reset_Control()
        {
            txtServiceName.Text = "";
            txtDesc.Text = "";
            txtPrice.Text = "";
            ddlService.SelectedValue = "-- Chọn dịch vụ --";
        }

        #region Load Service
        private void Load_Service()
        {
            DataTable dtbService = ServiceData.SelectAll();

            if ((dtbService != null) && (dtbService.Rows.Count > 0))
            {
                ddlService.DataSource = dtbService;
                ddlService.DataValueField = dtbService.Columns["ServiceID"].ToString();
                ddlService.DataTextField = dtbService.Columns["ServiceName"].ToString();
                ddlService.DataBind();
            }
            ddlService.Items.Insert(0, "-- Chọn dịch vụ --");
        }
        #endregion

        #region Load Service Price
        public void Load_ServicePrice(int servicePriceID)
        {
            ServicePriceInfo servicePriceInfo = ServicePriceData.SelectByServicePriceID(servicePriceID);
            txtServiceName.Text = servicePriceInfo.ServiceName;
            txtDesc.Text = servicePriceInfo.ServiceDescription;
            txtPrice.Text = servicePriceInfo.ServicePrice.ToString();
            ddlService.SelectedValue = servicePriceInfo.ServiceID.ToString();
            if (servicePriceInfo.ServiceStatus == true)
            {
                rbtActive.Checked = true;
            }
            else
            {
                rbtDeactive.Checked = true;
            }
            btnSubmit.Text = "Cập nhật";
        }
        #endregion


        private bool CheckInput(string name)
        {
            if (name == "")
            {
                lblMsg.Text = "Bạn chưa nhập Tên dịch vụ";
                txtServiceName.Focus();
                return false;
            }

            if (ddlService.SelectedValue.Contains("Chọn dịch vụ"))
            {
                lblMsg.Text = "Bạn chưa chọn Dịch vụ";
                return false;
            }
            return true;
        }

        #region Submit
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = txtServiceName.Text.Trim();
            string desc = txtDesc.Text.Trim();
            double price = 0;
            try
            {
                price = Convert.ToDouble(txtPrice.Text.Trim());
            }
            catch (System.Exception)
            {
                price = 0;
            }
            bool status = true;
            if (rbtActive.Checked)
            {
                status = true;
            }
            else
            {
                status = false;
            }

            if (CheckInput(name))
            {
                int serviceID = Convert.ToInt32(ddlService.SelectedValue);

                if (IsNew)
                {
                    ServicePriceInfo servicePriceInfo = new ServicePriceInfo();
                    servicePriceInfo.ServiceName = name;
                    servicePriceInfo.ServiceDescription = desc;
                    servicePriceInfo.ServicePrice = price;
                    servicePriceInfo.ServiceStatus = status;
                    servicePriceInfo.ServiceID = serviceID;

                    ServicePriceData.Insert(servicePriceInfo);
                }
                else
                {
                    int servicePriceID = Convert.ToInt32(Session["sesID"]);
                    ServicePriceInfo servicePriceInfo = ServicePriceData.SelectByServicePriceID(servicePriceID);
                    servicePriceInfo.ServiceName = name;
                    servicePriceInfo.ServiceDescription = desc;
                    servicePriceInfo.ServicePrice = price;
                    servicePriceInfo.ServiceStatus = status;
                    servicePriceInfo.ServiceID = serviceID;

                    ServicePriceData.Update(servicePriceInfo);
                }
                Response.Redirect("ServicePrice.aspx");
            }
        }
        #endregion       
    }
}